package com.ruoyi.ejkgl.ejkwz.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ejkgl.ejkwz.domain.Ejkwz;
import com.ruoyi.ejkgl.ejkwz.service.IEjkwzService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 二级库物资Controller
 *
 * @author huacai
 * @date 2024-12-09
 */
@RestController
@RequestMapping("/ejkgl/ejkwz")
public class EjkwzController extends BaseController {
    @Resource
    private IEjkwzService ejkwzService;

    /**
     * 查询二级库物资列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Ejkwz ejkwz) {
        startPage();
        List<Ejkwz> list = ejkwzService.selectEjkwzList(ejkwz);
        return getDataTable(list);
    }

    /**
     * 导出二级库物资列表
     */
    @Log(title = "二级库物资", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Ejkwz ejkwz) {
        List<Ejkwz> list = ejkwzService.selectEjkwzList(ejkwz);
        ExcelUtil<Ejkwz> util = new ExcelUtil<Ejkwz>(Ejkwz.class);
        util.exportExcel(response, list, "二级库物资数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Ejkwz> util = new ExcelUtil<Ejkwz>(Ejkwz.class);
        util.importTemplateExcel(response, "二级库物资数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "二级库物资", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Ejkwz> util = new ExcelUtil<Ejkwz>(Ejkwz.class);
        InputStream inputStream = file.getInputStream();
        List<Ejkwz> list = util.importExcel(inputStream);
        inputStream.close();
        int count = ejkwzService.batchInsertEjkwz(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取二级库物资详细信息
     */
    @GetMapping(value = "/{ejkwzId}")
    public AjaxResult getInfo(@PathVariable("ejkwzId") String ejkwzId) {
        return success(ejkwzService.selectEjkwzByEjkwzId(ejkwzId));
    }

    /**
     * 新增二级库物资
     */
    @Log(title = "二级库物资", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Ejkwz ejkwz) {
        return toAjax(ejkwzService.insertEjkwz(ejkwz));
    }

    /**
     * 修改二级库物资
     */
    @Log(title = "二级库物资", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Ejkwz ejkwz) {
        return toAjax(ejkwzService.updateEjkwz(ejkwz));
    }

    /**
     * 删除二级库物资
     */
    @Log(title = "二级库物资", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ejkwzIds}")
    public AjaxResult remove(@PathVariable String[] ejkwzIds) {
        return toAjax(ejkwzService.deleteEjkwzByEjkwzIds(ejkwzIds));
    }
}
